package com.atguigu.gmall.realtime.app.dwm;

import com.alibaba.fastjson.JSON;
import com.atguigu.gmall.realtime.app.BaseAppV2;
import com.atguigu.gmall.realtime.bean.OrderWide;
import com.atguigu.gmall.realtime.bean.PaymentInfo;
import com.atguigu.gmall.realtime.bean.PaymentWide;
import com.atguigu.gmall.realtime.util.MyKafkaUtil;
import com.atguigu.gmall.realtime.util.MyTimeUtil;
import org.apache.flink.api.common.eventtime.WatermarkStrategy;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.datastream.KeyedStream;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.co.ProcessJoinFunction;
import org.apache.flink.streaming.api.windowing.time.Time;
import org.apache.flink.util.Collector;

import java.time.Duration;
import java.util.Map;

/**
 * @Author lizhenchao@atguigu.cn
 * @Date 2021/3/20 10:16
 */
public class DWMPaymentWideApp extends BaseAppV2 {
    public static void main(String[] args) {
        new DWMPaymentWideApp().init(20004,
                                     2,
                                     "DWMPaymentWideApp",
                                     "DWMPaymentWideApp",
                                     "dwd_payment_info", "dwm_order_wide");
    }
    
    @Override
    public void run(StreamExecutionEnvironment env,
                    Map<String, DataStreamSource<String>> sourceStreams) {
        // 0.99999999999 == 1
        // 1. payment info 事实表
        final KeyedStream<PaymentInfo, Long> paymentInfoStream = sourceStreams
            .get("dwd_payment_info")
            .map(json -> JSON.parseObject(json, PaymentInfo.class))
            .assignTimestampsAndWatermarks(
                WatermarkStrategy
                    .<PaymentInfo>forBoundedOutOfOrderness(Duration.ofSeconds(5))
                    .withTimestampAssigner((paymentInfo, ts) -> MyTimeUtil.toTs(paymentInfo.getCreate_time(), "yyyy-MM-dd HH:mm:ss"))
            )
            .keyBy(PaymentInfo::getOrder_id);
        
        // 2. order wide 事实表
        final KeyedStream<OrderWide, Long> orderWideStream = sourceStreams
            .get("dwm_order_wide")
            .map(json -> JSON.parseObject(json, OrderWide.class))
            .assignTimestampsAndWatermarks(
                WatermarkStrategy
                    .<OrderWide>forBoundedOutOfOrderness(Duration.ofSeconds(5))
                    .withTimestampAssigner((orderWide, ts) -> MyTimeUtil.toTs(orderWide.getCreate_time(), "yyyy-MM-dd HH:mm:ss"))
            )
            .keyBy(OrderWide::getOrder_id);
        
        // 3. interval join
        paymentInfoStream
            .intervalJoin(orderWideStream)
            .between(Time.minutes(-45), Time.seconds(60))
            .process(new ProcessJoinFunction<PaymentInfo, OrderWide, PaymentWide>() {
                @Override
                public void processElement(PaymentInfo left,
                                           OrderWide right,
                                           Context ctx,
                                           Collector<PaymentWide> out) throws Exception {
                    out.collect(new PaymentWide(left, right));
                }
            })
            .map(JSON::toJSONString)
            .addSink(MyKafkaUtil.getFlinkKafkaSink("dwm_payment_wide"));
    }
}
